#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

# TODO
# Online or Offline，在线或者离线安装
# 在线安装，远程下载安装包
# 离线安装，检查本地安装包
# 配置config，如果playground已经初始化，则将配置项同步写入到配置文件frames.txt中保存

function installMaven()
{   
    #2.查看软件存放目录下是否有maven安装包
    plyIsExists=`find $PLAY_HOME -name SUCCESS`
    
    if [[ ${#plyIsExists} -ne 0 ]];then
        #在frames.txt中查看是否需要安装maven
        mvnInfo=`egrep "maven" $PLAY_HOME/frames.txt`
        
        mvn=`echo $mvnInfo | cut -d " " -f1`
        isInstall=`echo $mvnInfo | cut -d " " -f2`
        mvnNodes=`echo $mvnInfo | cut -d " " -f3` 
        softPath=$PLAY_HOME/frames
    else
        read -p "maven package name:  " mvn
        isInstall=true
        mvnNodes=`hostname` 
        softPath=`pwd`
    fi

    configureMaven $mvn $isInstall $mvnNodes $softPath
}

function configureMaven()
{
 #1.在frames.txt中查看是否需要安装maven
#  mvnInfo=`egrep "maven" $PLAY_HOME/frames.txt`
 
 mvn=$1
 isInstall=$2
 mvnNodes=$3
 softPath=$4

 node=`hostname`

 #是否安装
 if [[ $isInstall = "true" && $mvnNodes =~ $node ]];then
 
    #2.查看软件存放目录下是否有maven安装包
    mvnIsExists=`find $softPath -name $mvn`
    
    if [[ ${#mvnIsExists} -ne 0 ]];then
        
        if [ -d /usr/lib/maven ];then
              rm -rf /usr/lib/maven
        fi
 
        mkdir /usr/lib/maven && chmod -R 777 /usr/lib/maven
   
        #2.解压到指定文件夹/usr/lib/maven中 
        echo "开启解压maven安装包"
        tar -zxvf $mvnIsExists -C /usr/lib/maven >& /dev/null
        echo "maven安装包解压完毕"

        mvn_home=`find /usr/lib/maven/ -maxdepth 1 -name "apache-maven*"`

        #3.配置国内镜像源
        num=`sed -n -e "/<mirrors>/=" $mvn_home/conf/settings.xml`
        sed -i "${num}a <mirror>" $mvn_home/conf/settings.xml
        sed -i "`expr $num + 1`a <id>aliyunmaven<\/id>" $mvn_home/conf/settings.xml
        sed -i "`expr $num + 2`a <mirrorOf>*<\/mirrorOf>" $mvn_home/conf/settings.xml
        sed -i "`expr $num + 3`a <name>阿里云公共仓库<\/name>" $mvn_home/conf/settings.xml
        sed -i "`expr $num + 4`a <url>https://maven.aliyun.com/repository/public<\/url>" $mvn_home/conf/settings.xml
        sed -i "`expr $num + 5`a <\/mirror>" $mvn_home/conf/settings.xml
        
        #4.在/etc/profile配置MVN_HOME
        profile=/etc/profile
        sed -i "/^export MVN_HOME/d" $profile
        echo "export MVN_HOME=$mvn_home" >> $profile
 
        #5.在/etc/profile配置PATH
        sed -i "/^export PATH=\$PATH:\$MVN_HOME\/bin/d" $profile
        echo "export PATH=\$PATH:\$MVN_HOME/bin" >> $profile

        #6.更新/etc/profile文件
        source /etc/profile && source /etc/profile

        echo "success!"
    else
         echo "Maven安装包不存在"
    fi
 else
     echo "Maven不允许安装在当前节点"
 fi
}

installMaven
